window.addAddress||(addAddress= {
    /**
     * 省，市，区
     * **/
    address: [
        {
            name: 'country',
            num: 1
        },
        {
            name: 'province',
            num: 0
        },
        {
            name: 'city',
            num: ''
        },
        {
            name: 'district',
            num: ''
        }],
    init: function () {
        var me = this;
        if(ADDRESS.Code==200){
            me.address=[
                {
                    name: 'country',
                    num: 1
                },
                {
                    name: 'province',
                    num: ADDRESS.ua_province_id
                },
                {
                    name: 'city',
                    num: ADDRESS.ua_city_id
                },
                {
                    name: 'district',
                    num: ADDRESS.ua_county_id
                }
            ];
        }
        me.initAddress();
    },
    modifyAddress: function (id, panel, callback) {
        var me = this;
        $.ajax({
            type: "POST",
            url: "/request-conv/index.php?m=Address&a=autoAddress",
            dataType: 'json',
            data: {
                "id": id
            },
            success: function (result) {
                if (result.Code == 200) {
                    $(panel).html('');
                    $.each(result.address, function (i, item) {
                        $(panel).append('<option value="' + item.id + '">' + item.name + '</option>');
                    });
                    if (callback) callback(result.address);
                }
            }
        });
    },
    initAddress: function (indexs) {
        var me = this;
        if(!indexs){
            indexs=0;
        }
        var data=me.address[indexs],
            next=me.address[indexs+1];
        if(next) {
            var selectPanel=$('#' + next.name);
            me.modifyAddress(data.num, selectPanel, function (arr) {
                if (next) {
                    if (next.num != '') {
                        var cityStart = null;
                        $.each(arr, function (i, item) {
                            if (item.id == next.num) {
                                cityStart = item.id;
                                return false;
                            }
                        });
                        if (cityStart == null) {
                            next.num = arr[0].id;
                        }
                    } else {
                        next.num = arr[0].id;
                    }
                    selectPanel.val(next.num);
                    me.initAddress(indexs+1);
                }
            });
        }
    },
    changeProvince: function (t) {
        var me = this,
            that = $(t);
        me.address[1].num = that.val();
        me.initAddress(1);
    },
    changeCity: function (t) {
        var me = this,
            that = $(t);
        me.address[2].num = that.val();
        me.initAddress(2);
    },
    changeDistrict: function (t) {
        var me = this,
            that = $(t);
        me.address[3].num = that.val();
    },
    getElement:function () {
        var me=this;
        me.DOEM={
            name:$("#name"),
            tel:$("#tel"),
            province:$("#province"),
            city:$("#city"),
            district:$("#district"),
            Address_code:$("#Address_code"),
            Address_phone_number:$("#Address_phone_number"),
            Address_extension:$("#Address_extension"),
            Address_Detail_1:$("#Address_Detail_1"),
            email:$("#email"),
            zipcode:$("#zipcode"),
            rememberMe:$("#remember-me")
        };
    },
    postStart:true,
    post:function () {
        var me = this;
        if(me.postStart) {
            me.postStart=false;
            me.getElement();
            var msg = false;
            if (!me.DOEM.Address_Detail_1.val()) {
                msg = '用户地址所在的街道信息';
            }
            if (!me.DOEM.name.val()) {
                msg = '请输入联系人姓名';
            }
            if (me.DOEM.tel.val() && !me.DOEM.tel.val().isMobile()) {
                msg = '请输入联系人手机号';
            }
            var number = me.DOEM.Address_code.val() + "-" + me.DOEM.Address_phone_number.val();
            if (!number.isPhone()) {
                msg = '请输入正确的电话号码';
            }
            if (me.DOEM.email.val() && !me.DOEM.email.val().isEmail()) {
                msg = '请输入正确的邮箱地址';
            }
            if (msg) {
                CommonJs.alert.show({
                    model: 'error',
                    title: msg
                });
                me.postStart=true;
            } else {
                var url = "/request-conv/index.php?m=Address&a=addAddress",
                    postData = {
                        "u_id": CommonJs.cookie.get('u_id'),//"用户id",
                        "ua_province": me.DOEM.province.find("option:selected").text(),//"用户所属省",
                        "ua_province_id": me.DOEM.province.val(),//"用户所属省ID",
                        "ua_city": me.DOEM.city.find("option:selected").text(),//"用户所属城市",
                        "ua_city_id": me.DOEM.city.val(),//"用户所属城市ID",
                        "ua_county": me.DOEM.district.find("option:selected").text(),//"用户地址所在区县",
                        "ua_county_id": me.DOEM.district.val(),//"用户地址所在区县ID",
                        "ua_street": me.DOEM.Address_Detail_1.val(),//"用户地址所在的街道信息",
                        "ua_contact": me.DOEM.name.val(),//"用户地址联系人姓名",
                        "ua_mobile": me.DOEM.tel.val(),//"用户地址联系人手机号",
                        "ua_tel": number + ',' + me.DOEM.Address_extension.val(),//"用户地址中的联系电话(可空)",
                        "ua_email": me.DOEM.email.val(),//"用户地址中的电子邮件地址(可空)",
                        "ua_zipcode": me.DOEM.zipcode.val(),//"用户地址中的邮政编码(可空)",
                        "ua_default": me.DOEM.rememberMe.prop('checked') ? 1 : 0//"是否设置为默认地址，1为默认地址"
                    };
                if (window.location.href.getQueryJson().id) {
                    url = "/request-conv/index.php?m=Address&a=postModifyAddress";
                    postData.ua_id = window.location.href.getQueryJson().id;
                }
                $.ajax({
                    type: "POST",
                    url: url,
                    dataType: 'json',
                    data: postData,
                    success: function (result) {
                        me.postStart=true;
                        if (result.Code == 200) {
                            var alt = CommonJs.alert.show({
                                "id": "5",
                                "model": 'success1',
                                "title": "添加成功",
                                "detail": "",
                                "btn": "关注",
                                "href": "http://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzIwNzE3NjU1MA==&scene=124#wechat_redirect",
                                "footer_content": "关注海尔微信公众号获取更多权益"
                            });

                            alt.params.pane.find('.HrWx_GZ,.Model_Closed').click(function () {
                                window.history.go(-1);
                            });
                        } else {
                            CommonJs.alert.show({
                                model: 'error',
                                title: result.erMsg
                            });
                        }
                    },
                    error:function () {
                        me.postStart=true;
                    }
                });
            }
        }
    }
});
addAddress.init();

